import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

const double _kDatePickerHeight = 200.0;
const double _kDatePickerTitleHeight = 44.0;

typedef DateChangedCallback(Map curr);

class BottomSelect {
  static void showDatePicker(BuildContext context,
      {Map<String ,List<String>> content, DateChangedCallback onConfirm}) {
    Navigator.push(
      context,
      new SelectRoute(content, onConfirm),
    );
  }
}

class SelectRoute<T> extends PopupRoute<T> {

  Map<String ,List<String>> content ;

  Map curr=Map();
  final DateChangedCallback onConfirm;

  SelectRoute(this.content, this.onConfirm);

  @override
  Duration get transitionDuration => const Duration(milliseconds: 200);

  @override
  bool get barrierDismissible => true;

  @override
  final String barrierLabel = "123";

  @override
  Color get barrierColor => Colors.black54;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    Widget bottomSheet = new MediaQuery.removePadding(
      context: context,
      removeTop: true,
      child: new GestureDetector(
        child: new AnimatedBuilder(
          animation: animation,
          builder: (BuildContext context, Widget child) {
            return new ClipRect(
              child: new CustomSingleChildLayout(
                delegate: _BottomPickerLayout(animation.value,
                    showTitleActions: true),
                child: new GestureDetector(
                  child: Material(
                    color: Colors.transparent,
                    child: Container(
                      color: Colors.white,
                      child: Column(
                        children: <Widget>[
                          title(context),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: _buildTitle(content.keys.toList())
                            ,
                          ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: buildSelect(),
                          )
                        ],
                      ),
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
    return bottomSheet;
  }

  List<Widget> buildSelect() {
    List<Widget> c= List();
    List<String> keys=content.keys.toList();
    for(int i=0;i<keys.length;i++){
      c.add(_createColumn(content[keys[i]],keys[i]));
    }
    return c;
  }

  Widget  _createColumn( List<String> list,String key) {
    return  Expanded(
      flex: 1,
      child: Container(
        height: 150,
        child: CupertinoPicker(
          backgroundColor: Colors.white,
          scrollController:
          FixedExtentScrollController(initialItem: list.length),
          itemExtent: 40,
          onSelectedItemChanged: (int index) {
            setState(() {
              curr[key]=list[index];
            });
          },
          children: List.generate(list.length, (int index) {
            return Container(
              height: 20,
              alignment: Alignment.center,
              child: Text(
                list[index],
                style: TextStyle(color: Color(0xFF000046), fontSize: 16),
                textAlign: TextAlign.start,
              ),
            );
          }),
        ),
      ),
    );
  }

  Column title(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          height: _kDatePickerTitleHeight,
          decoration: BoxDecoration(color: Colors.white),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(
                height: _kDatePickerTitleHeight,
                child: FlatButton(
                  child: Text(
                    "取消",
                    style: TextStyle(
                      color: Theme.of(context).unselectedWidgetColor,
                      fontSize: 16.0,
                    ),
                  ),
                  onPressed: () => Navigator.pop(context),
                ),
              ),
              Container(
                height: _kDatePickerTitleHeight,
                child: FlatButton(
                  child: Text(
                    '确定',
                    style: TextStyle(
                      color: Theme.of(context).primaryColor,
                      fontSize: 16.0,
                    ),
                  ),
                  onPressed: () {
                    onConfirm(curr);
                    Navigator.pop(context);
                  },
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  List<Widget> _buildTitle(List<String> list) {
    List<Widget> c= List();
    c = list.map((tit){
      return Expanded(
        flex: 1,
        child: Container(
          alignment: Alignment.center,
          child: Text(tit,style: TextStyle(color: Colors.white.withOpacity(0.5)),),
        ),
      );
    }).cast<Widget>().toList();
    return c;
  }
}

class _BottomPickerLayout extends SingleChildLayoutDelegate {
  _BottomPickerLayout(this.progress, {this.itemCount, this.showTitleActions});

  final double progress;
  final int itemCount;
  final bool showTitleActions;

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    double maxHeight = _kDatePickerHeight;
    if (showTitleActions) {
      maxHeight += _kDatePickerTitleHeight;
    }

    return new BoxConstraints(
        minWidth: constraints.maxWidth,
        maxWidth: constraints.maxWidth,
        minHeight: 0.0,
        maxHeight: maxHeight);
  }

  @override
  Offset getPositionForChild(Size size, Size childSize) {
    double height = size.height - childSize.height * progress;
    return new Offset(0.0, height);
  }

  @override
  bool shouldRelayout(_BottomPickerLayout oldDelegate) {
    return progress != oldDelegate.progress;
  }
}
